<?php 
namespace Home\Controller;
use \Think\Controller;

//内容控制器
class ContentController extends Controller
{
    public function index()
    {
        //判断用户是否登录
        if (empty($_SESSION['user'])) {
            $this->error('请登录!',U('login/index'),1);
            exit;
        }
        //得到experience的id
        $id = $_GET['id'];
        //查询experience中的数据
        M('experience')->create();
        $data = M('experience')->where("id='$id'")->select();

        //查询category中的数据
        $category_id = $data[0]['category_id'];
        M('category')->create();
        $data1 = M('category')->field('path')->where("id='$category_id'")->select();
        //得到path
        $data1 = $data1[0]['path'];
        //分割path到数组中
        $pid = explode(",",$data1);
        //删除数组最后一个元素
        array_pop($pid);
        //删除数组第一个元素
        array_shift($pid);
        //将category_id压入数组中
        array_push($pid,$category_id);
        // 查询数组中相应id字段的name值
        for ($i=0; $i <count($pid) ; $i++) { 
            $data2 = M('category')->field('name')->where('id='.$pid[$i])->select();
            $data3[] = $data2[0];
        }

        //查询账户个人信息
        $uid = $data[0]['uid'];
        $this->assign('uid',$uid);
        M('user_info')->create();
        $data4 = M('user_info')->where("uid='$uid'")->select();
        M('user')->create();
        $data5 = M('user')->where("id='$uid'")->select();

        //查询作者相关的经验
        $data6 = M('experience')->where("uid='$uid'")->limit(5)->order('id desc')->select();

        //查询评论信息
        $data7 = M('comment')->where("eid='$id'")->select();

        //查找评论数
        $data8 = M('comment')->where("eid='$id'")->count();

        //查询回复评论
        M('reply')->create();
        $data9 = M('reply')->where("eid='$id'")->select();

        //查询收藏状态
        $uuid = $_SESSION['user']['uid'];
        $list1 = M('collect')->where("eid='$id' AND uid='$uuid'")->select();
        if (empty($list1)) {
            $this->assign('type',1);
        }else{
            $this->assign('type',2);
        }

        //查询点赞状态
        $list2 = M('zan')->where("uid='$uuid' AND articleid='$id'")->select();
        if (empty($list2)) {
            $this->assign('status',1);
        }else{
            $this->assign('status',2);
        }
        $zan_count = M('zan')->where("articleid='$id'")->count();

        //查询关注状态
        $fuid = $_SESSION['user']['uid'];
        $fs = M('fans')->where("uid='$uid' AND fuid='$fuid'")->find();
        if (empty($fs)) {
            $this->assign('tp',1);
        }else{
            $this->assign('tp',2);
        }

        //查询用户状态
        $ps = M('user_info')->where("uid='$fuid'")->field('status')->find();
        $this->assign('ps',$ps['status']);

        //查询消息状态
        $news_count = M('news')->where("suid='$fuid' AND status='1'")->count();
        $this->assign('news_count',$news_count);

        $this->assign('zan_count',$zan_count);
        $this->assign('name',$data3);
        $this->assign('pid',$pid);
        $this->assign('count',count($pid));
        $this->assign('list',$data);
        $this->assign('data4',$data4);
        $this->assign('data5',$data5);
        $this->assign('data6',$data6);
        $this->assign('data7',$data7);
        $this->assign('data8',$data8);
        $this->assign('data9',$data9);
        $this->display('Content/index');
    }
    
    //阅读次数
    public function reader()
    {
        $id = $_POST['id'];
        M('experience')->create();
        M('experience')->where("id='$id'")->setInc('reader');
        $reader = M('experience')->where("id='$id'")->field('reader')->select();
        $this->ajaxReturn($reader);
    }

    //点赞次数
    public function good()
    {
        $id = $_POST['id'];
        $uid = $_SESSION['user']['uid'];
        $data['uid'] = $uid;
        $data['articleid'] = $id;

        //查询数据表中是否已经点赞
        $zan = M('zan')->where("uid='$uid' AND articleid='$id'")->select();
        //查询文章发布者的uid
        $uinid = M('experience')->where("id='$id'")->find();
        $uinid = $uinid['uid'];
        if (empty($zan)) {
            M('zan')->create();
            M('zan')->add($data);
            //experience表中g_count+1
            M('experience')->where("id='$id'")->setInc('g_count');
            M('user_info')->where("uid='$uinid'")->setInc('exp','20');
            M('user_info')->where("uid='$uinid'")->setInc('gold','20');
            $zan_count = M('zan')->where("articleid='$id'")->count();
            $this->ajaxReturn($zan_count);
        }

    }

    //收藏
    public function collect()
    {
        $eid = $_POST['eid'];
        $uid = $_SESSION['user']['uid'];
        $data['eid'] = $eid;
        $data['uid'] = $uid;
        //查询文章发布者的uid
        $uinid = M('experience')->where("id='$eid'")->find();
        $uinid = $uinid['uid'];
        $list = M('collect')->where("eid='$eid' AND uid='$uid'")->select();
        if (empty($list)) {
                M('collect')->add($data);
                M('experience')->create();
                M('experience')->where("id='$eid'")->setInc('collector');
                M('user_info')->where("uid='$uinid'")->setInc('exp','50');
                M('user_info')->where("uid='$uid'")->setInc('exp','10');
                M('user_info')->where("uid='$uinid'")->setInc('gold','50');
        }else{
            M('collect')->where("eid='$eid' AND uid='$uid'")->delete();
            M('experience')->where("id='$eid'")->setDec('collector');
            M('user_info')->where("uid='$uinid'")->setDec('exp','50');
            M('user_info')->where("uid='$uid'")->setDec('exp','10');
            M('user_info')->where("uid='$uinid'")->setDec('gold','50');
        }
        
    }

    //推荐经验
    public function classify()
    {
        $cid = $_GET['cid'];

        M('experience')->create();
        $count = M('experience')->where("category_id='$cid'")->count();
        $mt = mt_rand(1,$count)-1;
        $data = M('experience')->where("category_id='$cid'")->limit("$mt,1")->select();
        $this->ajaxReturn($data);
    }

    //评论
    public function comment()
    {

        $time = date("Y-m-d H:i:s");
        $content = $_POST['text'];
        $uid = $_SESSION['user']['uid'];
        $eid = $_GET['eid'];
        $path = $_SESSION['user']['path'];
        $name = $_SESSION['user']['name'];

        $data['time'] = $time;
        $data['content'] = $content;
        $data['uid'] = $uid;
        $data['eid'] = $eid;
        $data['path'] = $path;
        $data['name'] = $name;

        //查询文章发布者的uid
        $uinid = M('experience')->where("id='$eid'")->find();
        $uinid = $uinid['uid'];

        M('comment')->create();
        if (M('comment')->add($data)>0) {
            M('user_info')->where("uid='$uinid'")->setInc('exp','20');
            M('user_info')->where("uid='$uid'")->setInc('exp','10');
            M('user_info')->where("uid='$uinid'")->setInc('gold','20');
            $this->success('评论成功!');
        }else{
            $this->error('评论失败!');
        }
        // $list = M('comment')->select();

    }

    //回复评论
    public function reply()
    {
            $time = date("Y-m-d H:i:s");
            $content = $_POST['text'];
            $uid = $_SESSION['user']['uid'];
            $coid = $_GET['coid'];
            $name = $_SESSION['user']['name'];
            M('comment')->create();
            $eid = M('comment')->where("id='$coid'")->field('eid')->find();

            $data['time'] = $time;
            $data['content'] = $content;
            $data['uid'] = $uid;
            $data['coid'] = $coid;
            $data['name'] = $name;
            $data['eid'] = $eid['eid'];


            M('reply')->create();
            if (M('reply')->add($data)>0) {
                $this->success('评论成功!');
            }else{
                $this->error('评论失败!');
            }
    }

    //加关注
    public function addAttention()
    {
        $uid = $_GET['uid'];
        $fuid = $_SESSION['user']['uid'];
        $data['uid'] = $uid;
        $data['fuid'] = $fuid;
        $user_info = M('user_info');


        if ($data['uid'] == $data['fuid']) {
            $this->error('自己不可以关注自己啊亲~');
            exit;
        }

        $fun = M('fans')->where("uid='$uid' AND fuid='$fuid'")->select();

        if (empty($fun)) {
            //被关注的人粉丝数+1 关注者关注数faned+1
            $user_info->where('uid ='.$data['uid'])->setInc('fans');

            $user_info->where('uid ='.$data['fuid'])->setInc('faned');
            // 两者数据都更新成功则执行下一步
            M('fans')->add($data);
        }
    }
}